package com.liu.sunnyday.utils;

import java.util.Random;

/**
 * chance！Time
 *
 * @author liuqian
 * @date 2022-06-23 17:01
 * 随机生成验证码
 * generate:生成[ˈdʒenəreɪt]
 * Validate:验证[ˈvælɪdeɪt]
 * code：密码,代码...
 */
public class ValidateCodeUtils {
    /**
     * 验证码
     *
     * @param length
     * @return length相当与一个判断，判断需要的验证码是4位还是6位
     * 然后创建一个code接受随机生成的验证码
     * 小于1000就的随机数就加1000，变为4位数
     */
    public static Integer generateValidateCode(Integer length) {

        Integer code = null;
        //验证码长度正确就进入判断
        if (length == 4) {
            code = new Random().nextInt(9999);//随机生成，最大为9999
            if (code < 1000) {
                code = code + 1000;
            }
        } else if (length == 6) {
            code = new Random().nextInt(999999);
            if (code < 100000) {
                code = code + 100000;
            }
        } else {
            new RuntimeException("验证码");
        }
        return code;
    }

    public static String generateValidateCode4String(int length){
        Random rdm=new Random();
        String hexString = Integer.toHexString(rdm.nextInt());
        String capstr = hexString.substring(0, length);
        return capstr;
    }
}
